package com.lucaswangdev.controller;

import com.lucaswangdev.common.AjaxResult;
import com.lucaswangdev.mapper.UserMapper;
import com.lucaswangdev.entity.User;
import com.lucaswangdev.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/add")
    public AjaxResult add(@RequestBody User user) {
        try {
            if(user.getUsername() == null || user.getSex() == null || user.getAddress() == null){
                return AjaxResult.error("请求参数不正确");
            }
            user.setUsername(user.getUsername());
            user.setSex(user.getSex());
            user.setAddress(user.getAddress());
            Integer number = userService.insert(user);
            System.out.println("number=>" + number);
            return AjaxResult.success();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }

    @RequestMapping("/update")
    public AjaxResult update(@RequestBody User user){
        try {
            // User user = new User();
            user.setId(user.getId());
            user.setUsername(user.getUsername());
            user.setSex(user.getSex());
            user.setAddress(user.getAddress());
            Integer integer = userService.update(user);
            return AjaxResult.success();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 查询全部
     * @return
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public AjaxResult findAllUser() {
        try {
            List<User> userList =  userService.findAll();
            return AjaxResult.success(userList);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 分页查询
     * @return
     */
    @RequestMapping("/findByPage")
    @ResponseBody
    public AjaxResult findByPage(@RequestBody User user) {
        try {
            List<User> userList =  userService.findByPage(user.getPageNo(), user.getPageSize());
            return AjaxResult.success(userList);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * @param
     * @return
     */
    @RequestMapping("/findUser")
    public AjaxResult findUser(@RequestBody User user) {
        try{
            User _user = userService.findUser(user.getId());
            return AjaxResult.success(_user);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除方法
     * @param user
     * @return
     */
    @RequestMapping("/delete")
    public AjaxResult delete(@RequestBody User user) {
        try{
            Integer integer = userService.delete(user.getId());
            return AjaxResult.success();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }

}
